package com.Studen.Dao;

import com.Studen.bean.User;
import com.Studen.util.Jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class LoginDaoMpl implements LoginDao {
    public List<User> login(User user) {
        /**
         * 这里写和数据库相关的操作
         * 1、jdbc六部曲
         * * 第一步；获取jdbc对象
         * * 第二步：获取数据库连接 写sql语句
         * * 第三步：获取数据库操作对象
         * * 第四步：执行SQL语句
         * * 第五步：处理查询结果
         * * 第六步：关闭资源 先开后关
         */
        List<User> list = new ArrayList<>();
        //第一步；获取jdbc文件
        Jdbc jdbc = new Jdbc();
        //第二步：获取数据库连接
        Connection connection = jdbc.Get();
        String sql = "SELECT * FROM `u_user` WHERE u_name=? and u_pwd=? ";
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            //第三步：获取数据库操作对象
            statement = connection.prepareStatement(sql);
            //第四步：执行SQL语句
            statement.setString(1, user.getU_name());
            statement.setString(2, user.getU_pwd());
            resultSet = statement.executeQuery();
            //第五步：处理查询结果
            if (resultSet.next()) {
                int u_id = resultSet.getInt("u_id");
                String u_pwd = resultSet.getString("u_pwd");
                String u_name = resultSet.getString("u_name");
                int u_sex = resultSet.getInt("u_sex");
                int u_role = resultSet.getInt("u_role");
                int u_isdelete = resultSet.getInt("u_isdelete");

                if (u_role == 1 && u_isdelete == 1) {
                    user.setU_id(u_id);
                    user.setU_pwd(u_pwd);
                    user.setU_name(u_name);
                    user.setU_sex(u_sex);
                    user.setU_isdelete(u_isdelete);
                    user.setU_role(u_role);
                    list.add(user);
                } else {
                    return null;
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        return list;
    }

    @Override
    public List<User> show() {
        List<User> list = new ArrayList<>();
        //第一步；获取jdbc文件
        Jdbc jdbc = new Jdbc();
        //第二步：获取数据库连接
        Connection connection = jdbc.Get();
        String sql = "SELECT * FROM `u_user`";
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            //第三步：获取数据库操作对象
            statement = connection.prepareStatement(sql);
            //第四步：执行SQL语句

            resultSet = statement.executeQuery();
            //第五步：处理查询结果
            while (resultSet.next()) {
                User user = new User();
                int u_id = resultSet.getInt("u_id");
                String u_pwd = resultSet.getString("u_pwd");
                String u_name = resultSet.getString("u_name");
                int u_sex = resultSet.getInt("u_sex");
                int u_role = resultSet.getInt("u_role");
                int u_isdelete = resultSet.getInt("u_isdelete");

                if (u_role==0 && u_isdelete==1) {
                    user.setU_id(u_id);
                    user.setU_pwd(u_pwd);
                    user.setU_name(u_name);
                    user.setU_sex(u_sex);
                    user.setU_isdelete(u_isdelete);
                    user.setU_role(u_role);
                    list.add(user);
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        return list;
    }
}
